package com.example.basemvvmkotlin.vm

import androidx.lifecycle.MutableLiveData
import com.example.basemvvmkotlin.net.InfoBean
import com.example.basemvvmkotlin.net.MyApiClient
import com.example.basemvvmkotlin.net.NewsListBean
import com.example.basemvvmkotlin.net.UserApiService

import com.example.lib_base.base.http.ApiResponse
import com.example.lib_base.base.vm.BaseViewModel
import com.example.lib_base.base.extensions.toast
import java.util.HashMap

class HomeVm : BaseViewModel() {

     var newsList = MutableLiveData<NewsListBean>()

     var info = MutableLiveData<InfoBean>()

     fun getInfo(){
        launchOnIO {
            println("--------------------- show")
            showLoadingUI(true)
            accessServer (NewsListBean::class) {
                MyApiClient.getInstance().getService(UserApiService::class.java).newsListBean(
                    mutableMapOf("page" to "1"))
            }
            accessServer(type = InfoBean::class){
                MyApiClient.getInstance().getService(UserApiService::class.java).hotPushList()
            }

            showLoadingUI(false)
            println("--------------------- dism")
        }
    }

     fun getNewsListBean(page:Int){

        launchOnIO {
            accessServer (NewsListBean::class) {
                MyApiClient.getInstance().getService(UserApiService::class.java).newsListBean(
                    mutableMapOf("page" to "1"))
            }
        }
    }

    override fun <B : Any> networkError(e: Throwable, type: B) {
        println("----------------- networkError1  $type")
        when (type){
              InfoBean::class -> {
                  "获取用户信息失败，请检查api接口是否正确".toast()
//                showToast("获取用户信息失败，请检查api接口是否正确")
            }
            else -> {
                super.networkError(e, type)
            }
        }
    }

    override fun <T : ApiResponse<*>> serverResponseObj(bean: T, code: Int, message: String) {

            when (bean.data){
                is NewsListBean -> {
                    println("--------------------- 列表数据")
                    newsList.postValue(bean.data as NewsListBean)
                }
                is InfoBean -> {
                    println("--------------------- 详情数据")
                    info.postValue(bean.data as InfoBean)
                }
                else -> {

                }
            }
    }
}